package api

import (
	"api-client/basic/config"
	__ "api-client/basic/proto"
	"api-client/handler/request"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

func UploadVideo(c *gin.Context) {
	file, _ := c.FormFile("file")
	log.Println(file.Filename)
	if file.Size <= 1024*1024*1024 {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "文件过大",
			"data": nil,
		})
		return
	}
	if file.Filename != ".mp4" && file.Filename != ".flv" {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "文件格式错误",
			"data": nil,
		})
		return
	}

	dst := "D:\\gowork\\src\\zg4\\2212a\\ks\\b-station\\upload_video\\" + file.Filename
	// 上传文件至指定的完整文件路径
	c.SaveUploadedFile(file, dst+file.Filename)
}

func AddVideo(c *gin.Context) {
	var req request.AddVideo
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "参数错误",
			"data": nil,
		})
		return
	}

	r, _ := config.ClientClient.AddVideo(c, &__.AddVideoReq{
		Title:       req.Title,
		Description: req.Description,
		Url:         req.Url,
		Category:    req.Category,
		UserId:      uint64(req.UserId),
	})

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "success",
		"data": r,
	})

}

func GetVideo(c *gin.Context) {
	var req request.GetVideo
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "参数错误",
			"data": nil,
		})
		return
	}

	r, _ := config.ClientClient.ListVideo(c, &__.ListVideoReq{
		VideoId: uint64(req.Id),
	})

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "success",
		"data": r,
	})
}
